package cn.school.attendance.dormitory.service.impl;

import cn.school.attendance.dormitory.dao.AttendanceRulesDao;
import cn.school.attendance.dormitory.entity.AttendanceRules;
import cn.school.attendance.dormitory.service.AttendanceRulesService;
import cn.school.common.utils.SysUserUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author: lx
 * @date: 2023/9/5 9:54
 * @description:
 */
@Service
public class AttendanceRulesServiceImpl
        extends ServiceImpl<AttendanceRulesDao, AttendanceRules>
        implements AttendanceRulesService {

    @Autowired
    AttendanceRulesDao attendanceRulesDao;

    @Override
    public IPage<AttendanceRules> findRulesPageList(AttendanceRules query) {
        String tenantId = SysUserUtils.getSysUser().getTenantId();
        query.setTenantId(tenantId);
        int count = attendanceRulesDao.findRulesPageListCount(query);
        int startNum = (query.getPageNum() - 1) * query.getPageSize();
        List<AttendanceRules> list = attendanceRulesDao.findRulesPageList(query,startNum,query.getPageSize());
        IPage<AttendanceRules> page = new Page<>();
        page.setTotal(count);
        page.setRecords(list);
        return page;
    }

    @Override
    public int addRules(AttendanceRules rules) {
        String tenantId = SysUserUtils.getSysUser().getTenantId();
        LambdaQueryWrapper<AttendanceRules> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AttendanceRules::getTimeSlot,rules.getTimeSlot());
        wrapper.eq(AttendanceRules::getTenantId,tenantId);
        AttendanceRules attendanceRules = this.baseMapper.selectOne(wrapper);
        if(attendanceRules != null){
            return -1;
        }
        return this.baseMapper.insert(rules);
    }

    @Override
    public int updateRules(AttendanceRules rules) {
        rules.setModifyTime(new Date());
        return this.baseMapper.updateById(rules);
    }

    @Override
    public int deleteRules(Long id) {
        return this.baseMapper.deleteById(id);
    }

    @Override
    public AttendanceRules findAttendanceRulesById(Long id) {
        return this.baseMapper.selectById(id);
    }
}
